var defaultDelay = 1000 * 90;
var minimumDelay = 1000 * 30;

var passwordChanged = false;
var showRandomMeme;

var whereToReadMeme = localStorage.whereToReadMeme || 'meemi';
var notification;
if( localStorage.notification == undefined
   || ( notification = JSON.parse( localStorage.notification )).toString() != '[object Object]' )
		notification = {
			'memes': false,
			'replies': false,
			'memes_priv': false,
			'replies_priv': false,
			'mentions': false,
			'new_followers': false };

		
//quando ci sarà la localizzazione questo array non sarà più necessario
var notificationLabels = {
	'memes':'Memi',
	'replies':'Risposte',
	'memes_priv':'Memi Privati',
	'replies_priv':'Risposte Private',
	'mentions':'Citazioni',
	'new_followers':'Followers' };
		
document.addEventListener( 'DOMContentLoaded', function() {

	var usernameTextBox = $( '#username' );
	var passwordTextBox = $( '#password' );
	var delayTextBox = $( '#delay-time' );
	var saveButton = $( '#save-button' );
	var radioShow = $( '#button-behaviour-show' );
	var radioHide = $( '#button-behaviour-hide' );

	// Draw checkboxes
	var position = $( '#checkboxes' );
	var checkbox;
	var checkLabel;
	$.each( notification, function( key, value ) {
		
		checkbox = $( '<input />' );
		checkbox.attr({
			'id': 'checkbox-' + key,
			'type': 'checkbox',
			'checked': value });
		checkbox.click( function() {
			notification[key] = $( '#checkbox-'+ key ).attr( 'checked' ) == 'checked';
			markDirty();
		});
		
		checkLabel = $( '<label />' );
		checkLabel.attr({ 
			'for': 'checkbox_' + key });
		checkLabel.append( notificationLabels[key] );
		position.append( checkbox );
		position.append( checkLabel );
		position.append( $( '<br />' ) );
	});
	// Compile page with existing setting
	usernameTextBox.val( localStorage.username || '' );
	passwordTextBox.val( localStorage.password || '' );
	delayTextBox.val( ( localStorage.requestDelay / 1000 ) || ( defaultDelay / 1000 ) );
	showRandomMeme = localStorage.showRandomMeme || 'show';
	
	var popupBehaviour = 'show';
	if( localStorage.showPopup == 0 )
		popupBehaviour = 'hide';
	$( 'input[name=button-popup-behaviour][value=' + popupBehaviour + ']' ).click();
			
	if( showRandomMeme == 'show' )
		$( '#button-random-behaviour' ).attr({
			'checked': true });
	markClean();

	$.getJSON( 'manifest.json', function( data ){
		$( '#span_version' ).append( data.version );});
	
	// ********************** //
	// Setting event listener //
	// ********************** //
	
	$( 'input' ).change( markDirty );
	$( 'input[name="button-popup-behaviour"]' ).click( setPopupBehaviour );
	$( '#button-random-behaviour').click( setRandomMemeBehaviour );
	
	$( '#save-button' ).click( function() {
		localStorage.username = usernameTextBox.val();
		if( passwordChanged )
			localStorage.password = $.sha256( passwordTextBox.val() );
		localStorage.notification = JSON.stringify( notification );
		localStorage.showRandomMeme = showRandomMeme;
			
		var intDelay = parseInt( delayTextBox.val() ) * 1000;
		if( isNaN( intDelay ) || intDelay < 1000 * 30 ) 
			localStorage.requestDelay = defaultDelay;
		else
			localStorage.requestDelay = intDelay;

		markClean();
		chrome.extension.sendMessage({ 'validation': 'reload' });
	});
});
				
function setRandomMemeBehaviour() {
	if( $( '#button-random-behaviour' ).attr( 'checked' ) == 'checked' )
		showRandomMeme = 'show';
	else
		showRandomMeme = 'hide';
}

function markDirty( sourceObject ) { 
	$( '#save-button' ).attr({
		'disabled': false }); 
	if( sourceObject && $( this ).attr( 'id' ) == 'password' ){
		passwordChanged = true;
	}
}
function markClean() { $( '#save-button' ).attr({ 'disabled': true }); }

function setPopup() {
	radioShow.checked = 1;
	radioHide.checked = 0;
	chrome.browserAction.setPopup({ popup: 'popup.html' });
	chrome.browserAction.setTitle({ title: 'Condividi su Meemi.' })
	localStorage.showPopup = 1;
}
function removePopup() {
	radioHide.checked = 1;
	radioShow.checked = 0;
	chrome.browserAction.setPopup({ popup: '' });
	chrome.browserAction.setTitle({ title: 'Go to Meemi Home Page.' });
	localStorage.showPopup = 0;
}

function setPopupBehaviour() {
	var radioValue = $('input[name=button-popup-behaviour]:checked').val();
	if( radioValue == 'show' ) {
		chrome.browserAction.setPopup({ popup: 'popup.html' });
		chrome.browserAction.setTitle({ title: 'Condividi su Meemi.' })
		localStorage.showPopup = 1; }
	else {
		chrome.browserAction.setPopup({ popup: '' });
		chrome.browserAction.setTitle({ title: 'Go to Meemi Home Page.' });
		localStorage.showPopup = 0; }
}

function setMemeReader() {
	whereToReadMeme = $('input[name=button-readmeme]:checked').val();
}
